﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Text;
using System.EnterpriseServices;
using System.Configuration;
using Models;
using Common;
using Langben.DAL;
using Langben.BLL;
using Langben.App.Models;
using System.Collections;
using System.Data;

namespace Langben.App.Controllers
{
    /// <summary>
    /// 物品表
    /// </summary>
    public class ts_goodsController : BaseController
    {

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [SupportFilter]
        public ActionResult Index()
        {
        
            return View();
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [SupportFilter]
        public ActionResult KCCXBForm()
        { 
            return View();
        }

        /// <summary>
        /// 物品积压报警
        /// </summary>
        /// <returns></returns>
        [SupportFilter]
        public ActionResult WPJYBJForm()
        { 
            return View();
        }


        /// <summary>
        /// 物品缺货报警
        /// </summary>
        /// <returns></returns>
        [SupportFilter]
        public ActionResult WPQHBJForm()
        {
            return View();
        }

        /// <summary>
        /// 收发存报表
        /// </summary>
        /// <returns></returns>
        [SupportFilter]
        public ActionResult SFCBBForm()
        {

            return View();
        }

        /// <summary>
        /// 物品库存帐报表
        /// </summary>
        /// <returns></returns>
        [SupportFilter]
        public ActionResult WPKCZBBForm()
        {

            return View();
        }
        
         /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public ActionResult IndexSef()
        {

            return View();
        }
        /// <summary>
        /// 异步加载数据
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页显示的行数</param>
        /// <param name="order">排序字段</param>
        /// <param name="sort">升序asc（默认）还是降序desc</param>
        /// <param name="search">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetData(string id, int page, int rows, string order, string sort, string search)
        { 
            int total = 0;
            if (id != "")
            {
                search += "c_class&"+id+"^";
            }
            List<ts_goods> queryData = m_BLL.GetByParam(id, page, rows, order, sort, search, ref total);
            return Json(new datagrid
            {
                total = total,
                rows = queryData.Select(s => new
                {
                    Id = s.Id
					,c_code = s.c_code
					,c_desc = s.c_desc
					,c_model = s.c_model
					,c_unit = s.c_unit
					,c_class = s.c_class
					,c_class1 = s.c_class1
					,c_vendor = s.c_vendor
					,c_base = s.c_base
					,f_pricelast = s.f_pricelast
					,f_upper = s.f_upper
					,f_lower = s.f_lower
					,c_py = s.c_py
					,c_comment = s.c_comment
					,i_sign = s.i_sign
					
                }

                    )
            });
        }



        /// <summary>
        /// 异步加载数据含金额
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页显示的行数</param>
        /// <param name="order">排序字段</param>
        /// <param name="sort">升序asc（默认）还是降序desc</param>
        /// <param name="search">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetSFCBBData(string search)
        {

            DateTime dtStart = Convert.ToDateTime("1900-10-10");
            DateTime dtEnd = DateTime.Now;
            //string DH = "";
            //string YSDH = "";

            if (search != null)
            {
                string[] searchStr = search.Split('^');
                if (searchStr[0].Split('&')[1].ToString() != "")
                {
                    dtStart = Convert.ToDateTime(searchStr[0].Split('&')[1]);
                }
                if (searchStr[1].Split('&')[1].ToString() != "")
                {
                    dtEnd = Convert.ToDateTime(searchStr[1].Split('&')[1]);
                }
                //if (searchStr[2].Split('&')[1].ToString() != "")
                //{
                //    DH = searchStr[2].Split('&')[1];
                //}
                //if (searchStr[3].Split('&')[1].ToString() != "")
                //{
                //    YSDH = searchStr[3].Split('&')[1];
                //}
            }

            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();
            ht.Add("@DTStart", dtStart);
            ht.Add("@DTEnd", dtEnd);
            #region
            //string sqlString = rx.SqlBack("sel_SFCBB");
            //string strwhere = "";
            //if (DH != "")
            //{
            //    strwhere += " and convert(varchar(20),i_no) like '%'+'" + DH + "'+'%' ";
            //}
            //if (YSDH != "")
            //{
            //    strwhere += " and c_orgno like '%'+'" + YSDH + "'+'%' ";
            //} 
            //sqlString = sqlString.Replace("@CC", strwhere);
            //DataSet ds = rx.DsDoSQL(sqlString, ht);
            #endregion
            DataSet ds = rx.ReaderDataSet("sel_SFCBB", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_code"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString(); 
                goods.RKSL = dr["RKSL"].ToString();
                goods.RKJE = dr["RKJE"].ToString();
                goods.CKSL = dr["CKSL"].ToString();
                goods.CKJE = dr["CKJE"].ToString();
                goods.SYSL = dr["SYSL"].ToString();
                goods.SYJE = dr["SYJE"].ToString();


                goods.QCSL = (ExchangeStrToDec(dr["QCRKSL"].ToString()) + ExchangeStrToDec(dr["QCCKSL"].ToString()) + ExchangeStrToDec(dr["QCSYSL"].ToString())).ToString();
                goods.QCJE = (ExchangeStrToDec(dr["QCRKJE"].ToString()) + ExchangeStrToDec(dr["QCCKJE"].ToString()) + ExchangeStrToDec(dr["QCSYJE"].ToString())).ToString();
                goods.QMSL = (ExchangeStrToDec(goods.QCSL) + ExchangeStrToDec(goods.RKSL) + ExchangeStrToDec(goods.CKSL) + ExchangeStrToDec(goods.SYSL)).ToString();
                goods.QMJE = (ExchangeStrToDec(goods.QCJE) + ExchangeStrToDec(goods.RKJE) + ExchangeStrToDec(goods.CKJE) + ExchangeStrToDec(goods.SYJE)).ToString();

                goodsList.Add(goods);
            }

            return Json(new datagrid
            {
                total = goodsList.Count(),
                rows = goodsList.Select(s => new
                {
                    c_code = s.c_code
                    ,
                    c_desc = s.c_desc
                    ,
                    c_model = s.c_model
                    ,
                    c_unit = s.c_unit
                    ,
                    QCSL = s.QCSL,
                    QCJE = s.QCJE,
                    RKSL = s.RKSL,
                    RKJE = s.RKJE,
                    CKSL = s.CKSL,
                    CKJE = s.CKJE,
                    SYSL = s.SYSL,
                    SYJE = s.SYJE,
                    QMSL = s.QMSL,
                    QMJE = s.QMJE

                }

                    )
            });
        }


        [HttpPost]
        public JsonResult PrintSFCBB(string search)
        {
            DateTime dtStart = Convert.ToDateTime("1900-10-10");
            DateTime dtEnd = DateTime.Now;
            //string DH = "";
            //string YSDH = "";

            if (search != null)
            {
                string[] searchStr = search.Split('^');
                if (searchStr[0].Split('&')[1].ToString() != "")
                {
                    dtStart = Convert.ToDateTime(searchStr[0].Split('&')[1]);
                }
                if (searchStr[1].Split('&')[1].ToString() != "")
                {
                    dtEnd = Convert.ToDateTime(searchStr[1].Split('&')[1]);
                }
                //if (searchStr[2].Split('&')[1].ToString() != "")
                //{
                //    DH = searchStr[2].Split('&')[1];
                //}
                //if (searchStr[3].Split('&')[1].ToString() != "")
                //{
                //    YSDH = searchStr[3].Split('&')[1];
                //}
            }

            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();
            ht.Add("@DTStart", dtStart);
            ht.Add("@DTEnd", dtEnd);
            #region
            //string sqlString = rx.SqlBack("sel_SFCBB");
            //string strwhere = "";
            //if (DH != "")
            //{
            //    strwhere += " and convert(varchar(20),i_no) like '%'+'" + DH + "'+'%' ";
            //}
            //if (YSDH != "")
            //{
            //    strwhere += " and c_orgno like '%'+'" + YSDH + "'+'%' ";
            //} 
            //sqlString = sqlString.Replace("@CC", strwhere);
            //DataSet ds = rx.DsDoSQL(sqlString, ht);
            #endregion
            DataSet ds = rx.ReaderDataSet("sel_SFCBB", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_code"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString();
                goods.RKSL = dr["RKSL"].ToString();
                goods.RKJE = dr["RKJE"].ToString();
                goods.CKSL = dr["CKSL"].ToString();
                goods.CKJE = dr["CKJE"].ToString();
                goods.SYSL = dr["SYSL"].ToString();
                goods.SYJE = dr["SYJE"].ToString();
                 
                goods.QCSL = (ExchangeStrToDec(dr["QCRKSL"].ToString()) + ExchangeStrToDec(dr["QCCKSL"].ToString()) + ExchangeStrToDec(dr["QCSYSL"].ToString())).ToString();
                goods.QCJE = (ExchangeStrToDec(dr["QCRKJE"].ToString()) + ExchangeStrToDec(dr["QCCKJE"].ToString()) + ExchangeStrToDec(dr["QCSYJE"].ToString())).ToString();
                goods.QMSL = (ExchangeStrToDec(goods.QCSL) + ExchangeStrToDec(goods.RKSL) + ExchangeStrToDec(goods.CKSL) + ExchangeStrToDec(goods.SYSL)).ToString();
                goods.QMJE = (ExchangeStrToDec(goods.QCJE) + ExchangeStrToDec(goods.RKJE) + ExchangeStrToDec(goods.CKJE) + ExchangeStrToDec(goods.SYJE)).ToString();
 
                goodsList.Add(goods);
            }


            //为打印提供html相关超文本内容。 
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border=1 cellSpacing=0 cellPadding=1 width=\"100%\" style=\"border-collapse:collapse;font-size:12px;line-height:30px\"  bordercolor=\"#333333\"> ");
            sb.Append("<thead><tr>");
            sb.Append("<td><div align=\"center\">编号</div></td>");
            sb.Append("<TD><div align=\"center\">名称</div></TD>");
            sb.Append("<TD><div align=\"center\">规格</div></TD>");
            sb.Append("<TD><div align=\"center\">单位</div></TD>");
            sb.Append("<TD><div align=\"center\">期初数量</div></TD>");
            sb.Append("<TD><div align=\"center\">期初金额</div></TD>");
            sb.Append("<TD><div align=\"center\">入库数量</div></TD>");
            sb.Append("<TD><div align=\"center\">入库金额</div></TD>");
            sb.Append("<TD><div align=\"center\">出库数量</div></TD>");
            sb.Append("<TD><div align=\"center\">出库金额</div></TD>");
            sb.Append("<TD><div align=\"center\">损溢数量</div></TD>");
            sb.Append("<TD><div align=\"center\">损溢金额</div></TD>");
            sb.Append("<TD><div align=\"center\">期末数量</div></TD>");
            sb.Append("<TD><div align=\"center\">期末金额</div></TD>");
            sb.Append("</TR>");
            sb.Append("</thead>");
            sb.Append("<tbody>");

             
            foreach (ts_goods item in goodsList)
            {
                sb.Append("<tr>");
                sb.Append("<td>");
                sb.Append(item.c_code);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_desc);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_model);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_unit);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QCSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QCJE);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.RKSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.RKJE);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.CKSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.CKJE);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.SYSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.SYJE);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMJE);
                sb.Append("</td>");

                sb.Append("</tr>"); 
            } 
            sb.Append("</tbody>");
            sb.Append("</TABLE>");

            return Json(new { success = true, data = sb.ToString() }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 异步加载数据仅数量
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页显示的行数</param>
        /// <param name="order">排序字段</param>
        /// <param name="sort">升序asc（默认）还是降序desc</param>
        /// <param name="search">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetSFCBBData2(string search)
        {

            DateTime dtStart = Convert.ToDateTime("1900-10-10");
            DateTime dtEnd = DateTime.Now;
            //string DH = "";
            //string YSDH = "";

            if (search != null)
            {
                string[] searchStr = search.Split('^');
                if (searchStr[0].Split('&')[1].ToString() != "")
                {
                    dtStart = Convert.ToDateTime(searchStr[0].Split('&')[1]);
                }
                if (searchStr[1].Split('&')[1].ToString() != "")
                {
                    dtEnd = Convert.ToDateTime(searchStr[1].Split('&')[1]);
                }
                //if (searchStr[2].Split('&')[1].ToString() != "")
                //{
                //    DH = searchStr[2].Split('&')[1];
                //}
                //if (searchStr[3].Split('&')[1].ToString() != "")
                //{
                //    YSDH = searchStr[3].Split('&')[1];
                //}
            }

            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();
            ht.Add("@DTStart", dtStart);
            ht.Add("@DTEnd", dtEnd);
            #region
            //string sqlString = rx.SqlBack("sel_SFCBB");
            //string strwhere = "";
            //if (DH != "")
            //{
            //    strwhere += " and convert(varchar(20),i_no) like '%'+'" + DH + "'+'%' ";
            //}
            //if (YSDH != "")
            //{
            //    strwhere += " and c_orgno like '%'+'" + YSDH + "'+'%' ";
            //} 
            //sqlString = sqlString.Replace("@CC", strwhere);
            //DataSet ds = rx.DsDoSQL(sqlString, ht);
            #endregion
            DataSet ds = rx.ReaderDataSet("sel_SFCBB", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_code"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString();
                goods.RKSL = dr["RKSL"].ToString(); 
                goods.CKSL = dr["CKSL"].ToString(); 
                goods.SYSL = dr["SYSL"].ToString(); 
                goods.QCSL = (ExchangeStrToDec(dr["QCRKSL"].ToString()) + ExchangeStrToDec(dr["QCCKSL"].ToString()) + ExchangeStrToDec(dr["QCSYSL"].ToString())).ToString(); 
                goods.QMSL = (ExchangeStrToDec(goods.QCSL) + ExchangeStrToDec(goods.RKSL) + ExchangeStrToDec(goods.CKSL) + ExchangeStrToDec(goods.SYSL)).ToString();
                goodsList.Add(goods);
            }

            return Json(new datagrid
            {
                total = goodsList.Count(),
                rows = goodsList.Select(s => new
                {
                    c_code = s.c_code
                    ,
                    c_desc = s.c_desc
                    ,
                    c_model = s.c_model
                    ,
                    c_unit = s.c_unit
                    ,
                    QCSL = s.QCSL, 
                    RKSL = s.RKSL, 
                    CKSL = s.CKSL, 
                    SYSL = s.SYSL, 
                    QMSL = s.QMSL 

                }

                    )
            });
        }


        [HttpPost]
        public JsonResult PrintSFCBB2(string search)
        {
            DateTime dtStart = Convert.ToDateTime("1900-10-10");
            DateTime dtEnd = DateTime.Now;
            //string DH = "";
            //string YSDH = "";

            if (search != null)
            {
                string[] searchStr = search.Split('^');
                if (searchStr[0].Split('&')[1].ToString() != "")
                {
                    dtStart = Convert.ToDateTime(searchStr[0].Split('&')[1]);
                }
                if (searchStr[1].Split('&')[1].ToString() != "")
                {
                    dtEnd = Convert.ToDateTime(searchStr[1].Split('&')[1]);
                }
                //if (searchStr[2].Split('&')[1].ToString() != "")
                //{
                //    DH = searchStr[2].Split('&')[1];
                //}
                //if (searchStr[3].Split('&')[1].ToString() != "")
                //{
                //    YSDH = searchStr[3].Split('&')[1];
                //}
            }

            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();
            ht.Add("@DTStart", dtStart);
            ht.Add("@DTEnd", dtEnd);
            #region
            //string sqlString = rx.SqlBack("sel_SFCBB");
            //string strwhere = "";
            //if (DH != "")
            //{
            //    strwhere += " and convert(varchar(20),i_no) like '%'+'" + DH + "'+'%' ";
            //}
            //if (YSDH != "")
            //{
            //    strwhere += " and c_orgno like '%'+'" + YSDH + "'+'%' ";
            //} 
            //sqlString = sqlString.Replace("@CC", strwhere);
            //DataSet ds = rx.DsDoSQL(sqlString, ht);
            #endregion
            DataSet ds = rx.ReaderDataSet("sel_SFCBB", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_code"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString();
                goods.RKSL = dr["RKSL"].ToString();
                goods.CKSL = dr["CKSL"].ToString();
                goods.SYSL = dr["SYSL"].ToString();
                goods.QCSL = (ExchangeStrToDec(dr["QCRKSL"].ToString()) + ExchangeStrToDec(dr["QCCKSL"].ToString()) + ExchangeStrToDec(dr["QCSYSL"].ToString())).ToString();
                goods.QMSL = (ExchangeStrToDec(goods.QCSL) + ExchangeStrToDec(goods.RKSL) + ExchangeStrToDec(goods.CKSL) + ExchangeStrToDec(goods.SYSL)).ToString();
                goodsList.Add(goods);
            }


            //为打印提供html相关超文本内容。 
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border=1 cellSpacing=0 cellPadding=1 width=\"100%\" style=\"border-collapse:collapse;font-size:12px;line-height:30px\"  bordercolor=\"#333333\"> ");
            sb.Append("<thead><tr>");
            sb.Append("<td><div align=\"center\">编号</div></td>");
            sb.Append("<TD><div align=\"center\">名称</div></TD>");
            sb.Append("<TD><div align=\"center\">规格</div></TD>");
            sb.Append("<TD><div align=\"center\">单位</div></TD>");
            sb.Append("<TD><div align=\"center\">期初数量</div></TD>"); 
            sb.Append("<TD><div align=\"center\">入库数量</div></TD>"); 
            sb.Append("<TD><div align=\"center\">出库数量</div></TD>"); 
            sb.Append("<TD><div align=\"center\">损溢数量</div></TD>"); 
            sb.Append("<TD><div align=\"center\">期末数量</div></TD>"); 
            sb.Append("</TR>");
            sb.Append("</thead>");
            sb.Append("<tbody>");


            foreach (ts_goods item in goodsList)
            {
                sb.Append("<tr>");
                sb.Append("<td>");
                sb.Append(item.c_code);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_desc);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_model);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_unit);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QCSL);
                sb.Append("</td>"); 

                sb.Append("<td>");
                sb.Append(item.RKSL);
                sb.Append("</td>"); 

                sb.Append("<td>");
                sb.Append(item.CKSL);
                sb.Append("</td>");
                 
                sb.Append("<td>");
                sb.Append(item.SYSL);
                sb.Append("</td>");
                  
                sb.Append("<td>");
                sb.Append(item.QMSL);
                sb.Append("</td>");
                 
                sb.Append("</tr>");
            }
            sb.Append("</tbody>");
            sb.Append("</TABLE>");

            return Json(new { success = true, data = sb.ToString() }, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 异步加载数据——库存查询表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页显示的行数</param>
        /// <param name="order">排序字段</param>
        /// <param name="sort">升序asc（默认）还是降序desc</param>
        /// <param name="search">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetKCCXBData(string search)
        {
            string c_id = "%";
            string c_class = "%";
            string c_base = "%";

            if (search != null)
            {
                string[] searchStr = search.Split('^');
                if (searchStr[0].Split('&')[1].ToString() != "")
                {
                    c_id = searchStr[0].Split('&')[1];
                }
                if (searchStr[1].Split('&')[1].ToString() != "")
                {
                    c_class = searchStr[1].Split('&')[1];
                }
                if (searchStr[2].Split('&')[1].ToString() != "")
                {
                    c_base = searchStr[2].Split('&')[1];
                } 
            }

            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();
            ht.Add("@c_id", c_id);
            ht.Add("@c_class", c_class);
            ht.Add("@c_base", c_base);

            DataSet ds = rx.ReaderDataSet("sel_KCCXB", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_desc"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString();
                goods.c_class = dr["c_class"].ToString();
                goods.QMSL = dr["QMSL"].ToString();
                goods.QMJE = dr["QMJE"].ToString();
                goods.f_pricelast = Convert.ToDecimal(dr["f_pricelast"].ToString());  
                goodsList.Add(goods);
            }

            return Json(new datagrid
            {
                total = goodsList.Count(),
                rows = goodsList.Select(s => new
                {
                    c_code = s.c_code
                    ,
                    c_desc = s.c_desc
                    ,
                    c_model = s.c_model
                    ,
                    c_unit = s.c_unit
                    ,
                    c_class = s.c_class,
                    QMSL = s.QMSL,
                    QMJE = s.QMJE,
                    f_pricelast = s.f_pricelast 

                }

                    )
            });
        }


        [HttpPost]
        public JsonResult PrintKCCXB(string search)
        {
            string c_id = "";
            string c_class = "";
            string c_base = "";

            if (search != null)
            {
                string[] searchStr = search.Split('^');
                if (searchStr[0].Split('&')[1].ToString() != "")
                {
                    c_id = searchStr[0].Split('&')[1];
                }
                if (searchStr[1].Split('&')[1].ToString() != "")
                {
                    c_class = searchStr[1].Split('&')[1];
                }
                if (searchStr[2].Split('&')[1].ToString() != "")
                {
                    c_base = searchStr[2].Split('&')[1];
                }
            }

            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();
            ht.Add("@c_id", c_id);
            ht.Add("@c_class", c_class);
            ht.Add("@c_base", c_base);

            DataSet ds = rx.ReaderDataSet("sel_KCCXB", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_desc"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString();
                goods.c_class = dr["RKSL"].ToString();
                goods.QMSL = dr["QMSL"].ToString();
                goods.QMJE = dr["QMJE"].ToString();
                goods.f_pricelast = Convert.ToDecimal(dr["f_pricelast"].ToString());
                goodsList.Add(goods);
            }

            //为打印提供html相关超文本内容。 
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border=1 cellSpacing=0 cellPadding=1 width=\"100%\" style=\"border-collapse:collapse;font-size:12px;line-height:30px\"  bordercolor=\"#333333\"> ");
            sb.Append("<thead><tr>");
            sb.Append("<td><div align=\"center\">编号</div></td>");
            sb.Append("<TD><div align=\"center\">名称</div></TD>");
            sb.Append("<TD><div align=\"center\">规格</div></TD>");
            sb.Append("<TD><div align=\"center\">单位</div></TD>");
            sb.Append("<TD><div align=\"center\">类别</div></TD>");
            sb.Append("<TD><div align=\"center\">数量</div></TD>");
            sb.Append("<TD><div align=\"center\">金额</div></TD>");
            sb.Append("<TD><div align=\"center\">库存均价</div></TD>"); 
            sb.Append("</TR>");
            sb.Append("</thead>");
            sb.Append("<tbody>");


            foreach (ts_goods item in goodsList)
            {
                sb.Append("<tr>");
                sb.Append("<td>");
                sb.Append(item.c_code);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_desc);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_model);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_unit);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_class);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMJE);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.f_pricelast);
                sb.Append("</td>");
                 
                sb.Append("</tr>");
            }
            sb.Append("</tbody>");
            sb.Append("</TABLE>");

            return Json(new { success = true, data = sb.ToString() }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 异步加载数据——物品积压报警
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页显示的行数</param>
        /// <param name="order">排序字段</param>
        /// <param name="sort">升序asc（默认）还是降序desc</param>
        /// <param name="search">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetWPJYBJData()
        { 
            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();

            DataSet ds = rx.ReaderDataSet("sel_KCJYBJ", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_desc"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString();
                goods.c_class = dr["c_class"].ToString();
                goods.QCSL = dr["QCSL"].ToString(); 
                goods.f_upper = Convert.ToDecimal(dr["f_upper"].ToString());
                goods.QMSL = Math.Abs((ExchangeStrToDec(goods.QMSL) - ExchangeStrToDec(goods.f_upper.ToString()))).ToString();//积压数量 
                goods.QMJE = (ExchangeStrToDec(goods.QMSL) * ExchangeStrToDec(dr["f_pricelast"].ToString())).ToString();//积压金额f_pricelast
                goodsList.Add(goods);
            }

            return Json(new datagrid
            {
                total = goodsList.Count(),
                rows = goodsList.Select(s => new
                {
                    c_code = s.c_code
                    ,
                    c_desc = s.c_desc
                    ,
                    c_model = s.c_model
                    ,
                    c_unit = s.c_unit
                    ,
                    c_class = s.c_class,
                    QMSL = s.QMSL,
                    QMJE = s.QMJE,
                    QCSL=s.QCSL,
                    f_upper=s.f_upper,
                    f_pricelast = s.f_pricelast

                }

                    )
            });
        }


        [HttpPost]
        public JsonResult PrintWPJYBJ()
        { 
            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();

            DataSet ds = rx.ReaderDataSet("sel_KCJYBJ", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_desc"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString();
                goods.c_class = dr["c_class"].ToString();
                goods.QCSL = dr["QCSL"].ToString();
                goods.f_upper = Convert.ToDecimal(dr["f_upper"].ToString());
                goods.QMSL = Math.Abs((ExchangeStrToDec(goods.QMSL) - ExchangeStrToDec(goods.f_upper.ToString()))).ToString();//积压数量 
                goods.QMJE = (ExchangeStrToDec(goods.QMSL) * ExchangeStrToDec(dr["f_pricelast"].ToString())).ToString();//积压金额f_pricelast
                goodsList.Add(goods);
            }

            //为打印提供html相关超文本内容。 
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border=1 cellSpacing=0 cellPadding=1 width=\"100%\" style=\"border-collapse:collapse;font-size:12px;line-height:30px\"  bordercolor=\"#333333\"> ");
            sb.Append("<thead><tr>");
            sb.Append("<td><div align=\"center\">编号</div></td>");
            sb.Append("<TD><div align=\"center\">名称</div></TD>");
            sb.Append("<TD><div align=\"center\">规格</div></TD>");
            sb.Append("<TD><div align=\"center\">单位</div></TD>");
            sb.Append("<TD><div align=\"center\">类别</div></TD>");
            sb.Append("<TD><div align=\"center\">库存上限</div></TD>");
            sb.Append("<TD><div align=\"center\">库存数量</div></TD>");
            sb.Append("<TD><div align=\"center\">积压数量</div></TD>");
            sb.Append("<TD><div align=\"center\">积压金额</div></TD>"); 
            sb.Append("</TR>");
            sb.Append("</thead>");
            sb.Append("<tbody>");


            foreach (ts_goods item in goodsList)
            {
                sb.Append("<tr>");
                sb.Append("<td>");
                sb.Append(item.c_code);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_desc);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_model);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_unit);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_class);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.f_upper);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QCSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMJE);
                sb.Append("</td>");

                sb.Append("</tr>");
            }
            sb.Append("</tbody>");
            sb.Append("</TABLE>");

            return Json(new { success = true, data = sb.ToString() }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 异步加载数据——物品缺货报警
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页显示的行数</param>
        /// <param name="order">排序字段</param>
        /// <param name="sort">升序asc（默认）还是降序desc</param>
        /// <param name="search">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetWPQHBJData()
        {
            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();

            DataSet ds = rx.ReaderDataSet("sel_KCQHBJ", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_desc"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString();
                goods.c_class = dr["c_class"].ToString();
                goods.QCSL = dr["QCSL"].ToString();
                goods.f_lower = Convert.ToDecimal(dr["f_lower"].ToString());
                goods.QMSL = Math.Abs((ExchangeStrToDec(goods.QMSL) - ExchangeStrToDec(goods.f_lower.ToString()))).ToString();//缺货数量 
                goods.QMJE = (ExchangeStrToDec(goods.QMSL) * ExchangeStrToDec(dr["f_pricelast"].ToString())).ToString();//缺货金额f_pricelast
                goodsList.Add(goods);
            }

            return Json(new datagrid
            {
                total = goodsList.Count(),
                rows = goodsList.Select(s => new
                {
                    c_code = s.c_code
                    ,
                    c_desc = s.c_desc
                    ,
                    c_model = s.c_model
                    ,
                    c_unit = s.c_unit
                    ,
                    c_class = s.c_class,
                    QMSL = s.QMSL,
                    QMJE = s.QMJE,
                    QCSL = s.QCSL,
                    f_lower = s.f_lower 

                }

                    )
            });
        }


        [HttpPost]
        public JsonResult PrintWPQHBJ()
        { 
            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();

            DataSet ds = rx.ReaderDataSet("sel_KCQHBJ", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_code = dr["c_code"].ToString();
                goods.c_desc = dr["c_desc"].ToString();
                goods.c_model = dr["c_model"].ToString();
                goods.c_unit = dr["c_unit"].ToString();
                goods.c_class = dr["c_class"].ToString();
                goods.QCSL = dr["QCSL"].ToString();
                goods.f_lower = Convert.ToDecimal(dr["f_lower"].ToString());
                goods.QMSL = Math.Abs((ExchangeStrToDec(goods.QMSL) - ExchangeStrToDec(goods.f_lower.ToString()))).ToString();//积压数量 
                goods.QMJE = (ExchangeStrToDec(goods.QMSL) * ExchangeStrToDec(dr["f_pricelast"].ToString())).ToString();//积压金额f_pricelast
                goodsList.Add(goods);
            }

            //为打印提供html相关超文本内容。 
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border=1 cellSpacing=0 cellPadding=1 width=\"100%\" style=\"border-collapse:collapse;font-size:12px;line-height:30px\"  bordercolor=\"#333333\"> ");
            sb.Append("<thead><tr>");
            sb.Append("<td><div align=\"center\">编号</div></td>");
            sb.Append("<TD><div align=\"center\">名称</div></TD>");
            sb.Append("<TD><div align=\"center\">规格</div></TD>");
            sb.Append("<TD><div align=\"center\">单位</div></TD>");
            sb.Append("<TD><div align=\"center\">类别</div></TD>");
            sb.Append("<TD><div align=\"center\">库存上限</div></TD>");
            sb.Append("<TD><div align=\"center\">库存数量</div></TD>");
            sb.Append("<TD><div align=\"center\">积压数量</div></TD>");
            sb.Append("<TD><div align=\"center\">积压金额</div></TD>");
            sb.Append("</TR>");
            sb.Append("</thead>");
            sb.Append("<tbody>");


            foreach (ts_goods item in goodsList)
            {
                sb.Append("<tr>");
                sb.Append("<td>");
                sb.Append(item.c_code);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_desc);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_model);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_unit);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_class);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.f_lower);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QCSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMJE);
                sb.Append("</td>");

                sb.Append("</tr>");
            }
            sb.Append("</tbody>");
            sb.Append("</TABLE>");

            return Json(new { success = true, data = sb.ToString() }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 异步加载数据——库存查询表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页显示的行数</param>
        /// <param name="order">排序字段</param>
        /// <param name="sort">升序asc（默认）还是降序desc</param>
        /// <param name="search">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetWPKCZBBData(string search)
        {
            DateTime dtStart = Convert.ToDateTime("1900-10-10");
            DateTime dtEnd = DateTime.Now;
            string c_code = "";
            string c_base = ""; 

            if (search != null)
            {
                string[] searchStr = search.Split('^');
                if (searchStr[0].Split('&')[1].ToString() != "")
                {
                    dtStart = Convert.ToDateTime(searchStr[0].Split('&')[1]);
                }
                if (searchStr[1].Split('&')[1].ToString() != "")
                {
                    dtEnd = Convert.ToDateTime(searchStr[1].Split('&')[1]);
                }
                if (searchStr[2].Split('&')[1].ToString() != "")
                {
                    c_base = searchStr[2].Split('&')[1];
                }
                if (searchStr[3].Split('&')[1].ToString() != "")
                {
                    c_code = searchStr[3].Split('&')[1];
                }
            }

            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();
            ht.Add("@DTStart", dtStart);
            ht.Add("@DTEnd", dtEnd);
            ht.Add("@c_code", c_code);
            ht.Add("@c_base", c_base);

            DataSet ds = rx.ReaderDataSet("sel_WPKCZBB", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_class = dr["c_type"].ToString();
                goods.dt_dt = dr["dt_dt"].ToString();
                goods.c_base = dr["c_base"].ToString();
                goods.RKSL = dr["RKSL"].ToString();
                goods.RKJE = dr["RKJE"].ToString();
                goods.CKSL = dr["CKSL"].ToString();
                goods.CKJE = dr["CKJE"].ToString();
                goods.QMSL = (Convert.ToInt32(dr["RKSL"].ToString())- Convert.ToInt32(dr["CKSL"].ToString())- Convert.ToInt32(dr["QMSL"].ToString())).ToString();
                goods.QMJE = (Convert.ToInt32(dr["RKJE"].ToString())-Convert.ToInt32(dr["CKJE"].ToString())- Convert.ToInt32(dr["QMJE"].ToString())).ToString();
                goods.c_comment = dr["c_comment"].ToString();
                goodsList.Add(goods);
            }

            return Json(new datagrid
            {
                total = goodsList.Count(),
                rows = goodsList.Select(s => new
                {
                    c_class = s.c_class,
                    dt_dt = s.dt_dt,
                    RKSL = s.RKSL,
                    RKJE = s.RKJE,
                    CKSL = s.CKSL,
                    CKJE = s.CKJE,
                    QMSL = s.QMSL,
                    QMJE = s.QMJE,
                    c_comment=s.c_comment

                }
                 )
            });
        }


        [HttpPost]
        public JsonResult PrintWPKCZBB(string search)
        {
            DateTime dtStart = Convert.ToDateTime("1900-10-10");
            DateTime dtEnd = DateTime.Now;
            string c_code = "";
            string c_base = "";

            if (search != null)
            {
                string[] searchStr = search.Split('^');
                if (searchStr[0].Split('&')[1].ToString() != "")
                {
                    dtStart = Convert.ToDateTime(searchStr[0].Split('&')[1]);
                }
                if (searchStr[1].Split('&')[1].ToString() != "")
                {
                    dtEnd = Convert.ToDateTime(searchStr[1].Split('&')[1]);
                }
                if (searchStr[2].Split('&')[1].ToString() != "")
                {
                    c_code = searchStr[2].Split('&')[1];
                }
                if (searchStr[3].Split('&')[1].ToString() != "")
                {
                    c_base = searchStr[3].Split('&')[1];
                }
            }

            Hashtable ht = new Hashtable();
            SqlReadXml rx = new SqlReadXml();
            ht.Add("@DTStart", dtStart);
            ht.Add("@DTEnd", dtEnd);
            ht.Add("@c_code", c_code);
            ht.Add("@c_base", c_base);

            DataSet ds = rx.ReaderDataSet("sel_WPKCZBB", ht);
            List<ts_goods> goodsList = new List<ts_goods>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ts_goods goods = new ts_goods();
                goods.c_class = dr["c_type"].ToString();
                goods.dt_dt = dr["dt_dt"].ToString();
                goods.c_base = dr["c_base"].ToString();
                goods.RKSL = dr["RKSL"].ToString();
                goods.RKJE = dr["RKJE"].ToString();
                goods.CKSL = dr["CKSL"].ToString();
                goods.CKJE = dr["CKJE"].ToString();
                goods.QMSL = dr["QMSL"].ToString();
                goods.QMJE = dr["QMJE"].ToString();
                goods.c_comment = dr["c_comment"].ToString();
                goodsList.Add(goods);
            }
             
            //为打印提供html相关超文本内容。 
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border=1 cellSpacing=0 cellPadding=1 width=\"100%\" style=\"border-collapse:collapse;font-size:12px;line-height:30px\"  bordercolor=\"#333333\"> ");
            sb.Append("<thead><tr>");
            sb.Append("<td><div align=\"center\">类型</div></td>");
            sb.Append("<TD><div align=\"center\">日期</div></TD>");
            sb.Append("<TD><div align=\"center\">仓库</div></TD>");
            sb.Append("<TD><div align=\"center\">进库数量</div></TD>");
            sb.Append("<TD><div align=\"center\">进库金额</div></TD>");
            sb.Append("<TD><div align=\"center\">出库数量</div></TD>");
            sb.Append("<TD><div align=\"center\">出库金额</div></TD>");
            sb.Append("<TD><div align=\"center\">结存数量</div></TD>");
            sb.Append("<TD><div align=\"center\">结存金额</div></TD>");
            sb.Append("<TD><div align=\"center\">备注</div></TD>");
            sb.Append("</TR>");
            sb.Append("</thead>");
            sb.Append("<tbody>");


            foreach (ts_goods item in goodsList)
            {
                sb.Append("<tr>");
                sb.Append("<td>");
                sb.Append(item.c_class);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.dt_dt);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_base);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.RKSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.RKJE);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.CKSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.CKJE);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMSL);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.QMJE);
                sb.Append("</td>");

                sb.Append("<td>");
                sb.Append(item.c_comment);
                sb.Append("</td>");

                sb.Append("</tr>");
            }
            sb.Append("</tbody>");
            sb.Append("</TABLE>");

            return Json(new { success = true, data = sb.ToString() }, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 异步加载数据
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页显示的行数</param>
        /// <param name="order">排序字段</param>
        /// <param name="sort">升序asc（默认）还是降序desc</param>
        /// <param name="search">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetDataByClass(string className)
        {
            List<ts_goods> queryData;
            if (className == "")
            {
                queryData = m_BLL.GetAll();
            }
            else
            {
                queryData = m_BLL.GetAll().Where(o => o.c_class == className).ToList();
            }
            
          
            return Json(new { success = true, data = queryData  }, JsonRequestBehavior.AllowGet);
             
        }

        public decimal ExchangeStrToDec(string val)
        {
            decimal returnval = 0;
            if (val != "")
            {
                returnval = Convert.ToDecimal(val);
            }
            return returnval;
        }


        /// <summary>
        /// 查看详细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [SupportFilter]  
        public ActionResult Details(string id)
        {
            ts_goods item = m_BLL.GetById(id);
            return View(item);

        }
 
        /// <summary>
        /// 首次创建
        /// </summary>
        /// <returns></returns>
        [SupportFilter]
        public ActionResult Create(string id)
        { 
            
            return View();
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [SupportFilter]
        public ActionResult Create(ts_goods entity)
        {           
            if (entity != null && ModelState.IsValid)
            {
                string currentPerson = GetCurrentPerson();
                //entity.CreateTime = DateTime.Now;
                //entity.CreatePerson = currentPerson;
              
                entity.Id = Result.GetNewId();   
                string returnValue = string.Empty;
                if (m_BLL.Create(ref validationErrors, entity))
                {
                    LogClassModels.WriteServiceLog(Suggestion.InsertSucceed  + "，物品表的信息的Id为" + entity.Id,"物品表"
                        );//写入日志 
                    return Json(Suggestion.InsertSucceed);
                }
                else
                { 
                    if (validationErrors != null && validationErrors.Count > 0)
                    {
                        validationErrors.All(a =>
                        {
                            returnValue += a.ErrorMessage;
                            return true;
                        });
                    }
                    LogClassModels.WriteServiceLog(Suggestion.InsertFail + "，物品表的信息，" + returnValue,"物品表"
                        );//写入日志                      
                    return Json(Suggestion.InsertFail  + returnValue); //提示插入失败
                }
            }

            return Json(Suggestion.InsertFail + "，请核对输入的数据的格式"); //提示输入的数据的格式不对 
        }
        /// <summary>
        /// 首次编辑
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns> 
        [SupportFilter] 
        public ActionResult Edit(string id)
        {
            ts_goods item = m_BLL.GetById(id);
            return View(item);
        }

        /// <summary>
        /// 提交编辑信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="collection">客户端传回的集合</param>
        /// <returns></returns>
        [HttpPost]
        [SupportFilter]
        public ActionResult Edit(string id, ts_goods entity)
        {
            if (entity != null && ModelState.IsValid)
            {   //数据校验
            
                string currentPerson = GetCurrentPerson();                 
                //entity.UpdateTime = DateTime.Now;
                //entity.UpdatePerson = currentPerson;
                           
                string returnValue = string.Empty;   
                if (m_BLL.Edit(ref validationErrors, entity))
                {
                    LogClassModels.WriteServiceLog(Suggestion.UpdateSucceed + "，物品表信息的Id为" + id,"物品表"
                        );//写入日志                           
                    return Json(Suggestion.UpdateSucceed); //提示更新成功 
                }
                else
                { 
                    if (validationErrors != null && validationErrors.Count > 0)
                    {
                        validationErrors.All(a =>
                        {
                            returnValue += a.ErrorMessage;
                            return true;
                        });
                    }
                    LogClassModels.WriteServiceLog(Suggestion.UpdateFail + "，物品表信息的Id为" + id + "," + returnValue, "物品表"
                        );//写入日志                           
                    return Json(Suggestion.UpdateFail  + returnValue); //提示更新失败
                }
            }
            return Json(Suggestion.UpdateFail + "请核对输入的数据的格式"); //提示输入的数据的格式不对               
          
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>   
        [HttpPost]
        public ActionResult Delete(FormCollection collection)
        {
            string returnValue = string.Empty;
            string[] deleteId = collection["query"].GetString().Split(',');
            if (deleteId != null && deleteId.Length > 0)
            { 
                if (m_BLL.DeleteCollection(ref validationErrors, deleteId))
                {
                    LogClassModels.WriteServiceLog(Suggestion.DeleteSucceed + "，信息的Id为" + string.Join(",", deleteId), "消息"
                        );//删除成功，写入日志
                    return Json("OK");
                }
                else
                {
                    if (validationErrors != null && validationErrors.Count > 0)
                    {
                        validationErrors.All(a =>
                        {
                            returnValue += a.ErrorMessage;
                            return true;
                        });
                    }
                    LogClassModels.WriteServiceLog(Suggestion.DeleteFail + "，信息的Id为" + string.Join(",", deleteId)+ "," + returnValue, "消息"
                        );//删除失败，写入日志
                }
            }
            return Json(returnValue);
        }
     
        IBLL.Its_goodsBLL m_BLL;

        ValidationErrors validationErrors = new ValidationErrors();

        public ts_goodsController()
            : this(new ts_goodsBLL()) { }

        public ts_goodsController(ts_goodsBLL bll)
        {
            m_BLL = bll;
        }
        
    }
}


